package com.github.yeecode.mybatisdemo.reflect;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : wushikai
 * <p>
 * date : 2021-01-16
 */

interface Aggregate {
    public abstract Iterator iterator();
}


interface Iterator {
    public abstract boolean hasNext();

    public abstract Object next();
}


class Book {
    private String name;

    public Book(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}



class BookShelfIterator implements Iterator {

    private BookShelf bookShelf;
    private int index;

    public BookShelfIterator(BookShelf bookShelf) {
        this.bookShelf = bookShelf;
        this.index = 0;
    }

    public boolean hasNext() {
        if (index < bookShelf.getLength()) {
            return true;
        } else {
            return false;
        }
    }


    public Object next() {
        Book book = bookShelf.getBookAt(index);
        index++;
        return book;
    }
}


public class BookShelf implements Aggregate {

    private List<Book> books;


    public BookShelf() {
        this.books = new ArrayList<Book>();
    }

    public Book getBookAt(int index) {
        return books.get(index);
    }

    public void appendBook(Book book) {
        books.add(book);
    }

    public int getLength() {
        return books.size();
    }

    public Iterator iterator() {
        return new BookShelfIterator(this);
    }


    public static void main(String[] args) {
        BookShelf bookShelf = new BookShelf();
        bookShelf.appendBook(new Book("java 编程思想"));
        bookShelf.appendBook(new Book("Java 高并发实战"));
        bookShelf.appendBook(new Book("springboot 实战"));
        bookShelf.appendBook(new Book("redis指南"));
        Iterator it = bookShelf.iterator();
        while (it.hasNext()) {
            Book book = (Book) it.next();
            System.out.println(book.getName());
        }
    }


}


